package com.gbs.info2.tools;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import com.gbs.info2.obj.VehicleMobile;

public class DatabaseHelper {

	private static final String DATABASE_NAME = "gbsi2.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "table1";
	private Context context;
	private SQLiteDatabase db;
	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into "
			+ TABLE_NAME
			+ "(title, ecv, fuelType, locationType, pin, specification, failures, central_lock, remote_control, alarm, spare_keys, vehicle_photo) values (?,?,?,?,?,?,?,?,?,?,?,?)";

	public DatabaseHelper(Context context) {
		this.context = context;
		OpenHelper openHelper = new OpenHelper(this.context);
		this.db = openHelper.getWritableDatabase();
		this.insertStmt = this.db.compileStatement(INSERT);
	}

	public long insert(VehicleMobile vehicleMobile) {
		this.insertStmt.bindString(1, vehicleMobile.getTitle());
		this.insertStmt.bindString(2, vehicleMobile.getEcv());
		this.insertStmt.bindString(3, vehicleMobile.getFuelType());
		this.insertStmt.bindString(4, vehicleMobile.getLocationType());
		this.insertStmt.bindString(5, vehicleMobile.getPin());
		this.insertStmt.bindString(6, vehicleMobile.getSpecification());
	    this.insertStmt.bindString(7, vehicleMobile.getFailures());
		this.insertStmt.bindDouble(8, vehicleMobile.getCentralLock());
		this.insertStmt.bindDouble(9, vehicleMobile.getRemoteControl());
		this.insertStmt.bindDouble(10, vehicleMobile.getSpareKeys());
		this.insertStmt.bindDouble(11, vehicleMobile.getAlarm());
		this.insertStmt.bindBlob(12, vehicleMobile.getVehiclePhoto());

		return this.insertStmt.executeInsert();
	}

	public void deleteAll() {
		this.db.delete(TABLE_NAME, null, null);
	}

	public ArrayList<VehicleMobile> selectAll(String whereCause) {
		ArrayList<VehicleMobile> vehicleMobiles = new ArrayList<VehicleMobile>();
		Cursor cursor = this.db.query(TABLE_NAME, new String[] { "title",
				"ecv", "fuelType", "locationType", "pin", "specification",
				"failures", "central_lock", "remote_control", "alarm",
				"spare_keys", "vehicle_photo" }, whereCause, null, null, null,
				"title");
		if (cursor.moveToFirst()) {
			do {
				VehicleMobile vehicleMobile = new VehicleMobile();
				vehicleMobile.setTitle(cursor.getString(0));
				vehicleMobile.setEcv(cursor.getString(1));
				vehicleMobile.setFuelType(cursor.getString(2));
				vehicleMobile.setLocationType(cursor.getString(3));
				vehicleMobile.setPin(cursor.getString(4));
				vehicleMobile.setSpecification(cursor.getString(5));
				vehicleMobile.setFailures(cursor.getString(6));
				vehicleMobile.setCentralLock(cursor.getInt(7));
				vehicleMobile.setRemoteControl(cursor.getInt(8));
				vehicleMobile.setSpareKeys(cursor.getInt(9));
				vehicleMobile.setAlarm(cursor.getInt(10));
				vehicleMobile.setVehiclePhoto(cursor.getBlob(11));
				vehicleMobiles.add(vehicleMobile);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return vehicleMobiles;
	}

	private static class OpenHelper extends SQLiteOpenHelper {

		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE "
					+ TABLE_NAME
					+ "(id INTEGER PRIMARY KEY, title TEXT, ecv TEXT, fuelType TEXT, locationType TEXT, pin TEXT, specification TEXT, failures TEXT,  central_lock INTEGER, remote_control INTEGER, alarm INTEGER, spare_keys INTEGER, vehicle_photo BLOB)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("Example",
					"Upgrading database, this will drop tables and recreate.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}

}